package com.example.wms;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import com.example.classes.*;

/**
 * Created with IntelliJ IDEA.
 * User: ankit
 * Date: 3/5/13
 * Time: 3:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class registerActivity extends Activity {
    public Database db;
    PopupWindow fail;
    boolean click;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GlobalApplication ga = (GlobalApplication)getApplicationContext();
        db = ga.getDb();
        setContentView(R.layout.register_screen);
    }
    public void tryRegister(View view)
    {
        db=retrieveData();
        try{
        db.open();
        db.open();
        }catch(Exception e){

        }

        String uname=((EditText)findViewById(R.id.registerUserID)).getText().toString();
        if(db.getEntry(uname)==null)
        {
            String upass=((EditText)findViewById(R.id.registerPassword)).getText().toString();
            String confirm=((EditText)findViewById(R.id.registerPassword2)).getText().toString();
            if(upass.equals(confirm))
            {
                upass=Encryptor.encrypt(upass);
                String email=((EditText)findViewById(R.id.registerEmail)).getText().toString();
                User newUser= new User(uname,upass,email);
                db.createUserEntry(newUser);
                db.close();
                finish();
            }
            else {
                Toast.makeText(this,"Passwords dont match!",Toast.LENGTH_LONG).show();
            }
        }
        else
        {
            Toast.makeText(this,"Username already exists",Toast.LENGTH_LONG).show();
        }
    }
    private Database retrieveData()
    {
        GlobalApplication ga = (GlobalApplication)getApplicationContext();
        db = ga.getDb();
        return db;
    }
}